package com.easylive.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.easylive.annotation.GlobalInterceptor;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.PageSize;
import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.entity.enums.UserActionTypeEnum;
import com.easylive.entity.enums.VideoOrderTypeEnum;
import com.easylive.entity.po.*;
import com.easylive.entity.query.*;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.UserInfoVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/uhome/series")
@Validated
public class UHomeVideoSeriesController extends ABaseController{
    @Resource
    private UserInfoService userInfoService;

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private UserActionService userActionService;

    @Resource
    private UserFocusService userFocusService;

    @Resource
    private UserVideoSeriesService userVideoSeriesService;

    @Resource
    private UserVideoSeriesVideoService userVideoSeriesVideoService;

    @RequestMapping("/loadVideoSeries")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadVideoSeries(@NotEmpty String userId){
        List<UserVideoSeries> list = userVideoSeriesService.getUserAllSeries(userId);

        return getSuccessResponseVO(list);
    }

    @RequestMapping("/saveVideoSeries")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveVideoSeries(@NotEmpty @Size(max = 100) String seriesName,
                                      Integer seriesId,
                                      @Size(max=200) String seriesDescription,
                                      String videoIds){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserVideoSeries userVideoSeries = new UserVideoSeries();
        userVideoSeries.setSeriesName(seriesName);
        userVideoSeries.setSeriesDescription(seriesDescription);
        userVideoSeries.setUserId(tokenUserInfoDto.getUserId());
        if(seriesId != null){
            userVideoSeries.setSeriesId(seriesId);
        }
        this.userVideoSeriesService.saveUserVideoSeries(userVideoSeries,videoIds);


        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadAllVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadAllVideo(Integer seriesId){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoInfoQuery query = new VideoInfoQuery();
        if(seriesId != null){
            UserVideoSeriesVideoQuery userVideoSeriesVideoQuery = new UserVideoSeriesVideoQuery();
            userVideoSeriesVideoQuery.setSeriesId(seriesId);
            userVideoSeriesVideoQuery.setUserId(tokenUserInfoDto.getUserId());
            List<UserVideoSeriesVideo> list = userVideoSeriesVideoService.findListByParam(userVideoSeriesVideoQuery);
            List<String> videoIds = list.stream().map(item -> item.getVideoId()).collect(Collectors.toList());
            query.setExcludeVideoIds(videoIds.toArray(new String[videoIds.size()]));
        }
        query.setUserId(tokenUserInfoDto.getUserId());
        List<VideoInfo> list = videoInfoService.findListByParam(query);

        return getSuccessResponseVO(list);
    }

    @RequestMapping("/getVideoSeriesDetail")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getVideoSeriesDetail(@NotNull Integer seriesId){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserVideoSeries userVideoSeries = userVideoSeriesService.getUserVideoSeriesBySeriesId(seriesId);
        if(userVideoSeries == null){
            throw new BusinessException(ResponseCodeEnum.CODE_404);
        }
        UserVideoSeriesVideoQuery videoQuery = new UserVideoSeriesVideoQuery();
        videoQuery.setOrderBy("sort asc");
        videoQuery.setQueryVideoInfo(true);
        videoQuery.setSeriesId(seriesId);
        List<UserVideoSeriesVideo> list = userVideoSeriesVideoService.findListByParam(videoQuery);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("seriesVideoList",list);
        resultMap.put("videoSeries",userVideoSeries);
        return getSuccessResponseVO(resultMap);
    }

    @RequestMapping("/saveSeriesVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveSeriesVideo(@NotNull Integer seriesId,
                                      @NotEmpty String videoIds){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.saveSeriesVideo(tokenUserInfoDto.getUserId(),videoIds,seriesId);


        return getSuccessResponseVO(null);
    }


    @RequestMapping("/delSeriesVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delSeriesVideo(@NotNull Integer seriesId,
                                      @NotEmpty String videoId){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.delSeriesVideo(tokenUserInfoDto.getUserId(),videoId,seriesId);


        return getSuccessResponseVO(null);
    }

    @RequestMapping("/delVideoSeries")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delVideoSeries(@NotNull Integer seriesId){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.delVideoSeries(tokenUserInfoDto.getUserId(),seriesId);


        return getSuccessResponseVO(null);
    }

    @RequestMapping("/changeVideoSeriesSort")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO changeVideoSeriesSort(@NotNull String seriesIds){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.changeVideoSeriesSort(tokenUserInfoDto.getUserId(),seriesIds);


        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadVideoSeriesWithVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadVideoSeriesWithVideo(@NotEmpty String userId){
        UserVideoSeriesQuery query = new UserVideoSeriesQuery();
        query.setUserId(userId);
        query.setOrderBy("sort asc");
        List<UserVideoSeries> list = userVideoSeriesService.findListWithVideoByParam(query);




        return getSuccessResponseVO(list);
    }


}
